﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MicroMvvm;
using Win10WallpaperFetcher.Model;

namespace Win10WallpaperFetcher.ViewModel
{
    public class FetcherViewModel : ObservableObject
    {

        public FetcherViewModel()
        {
            SetDefaultParam();
        }


        private bool _isOnlyFetchHorizontal;
        private bool _isOnlyFetchVertical;
        private bool _isFetchBoth;
        private int _minWidth;
        private int _minHeight;
        private string _saveFilePath;

        public static int MinSize => 100;
        public static int MaxSize => 5000;

        public bool IsOnlyFetchHorizontal
        {
            get => _isOnlyFetchHorizontal;
            set
            {
                if (_isOnlyFetchHorizontal != value)
                {
                    _isOnlyFetchHorizontal = value;
                    OnPropertyChanged();
                }
            }
        }
     
        public bool IsOnlyFetchVertical
        {
            get => _isOnlyFetchVertical;
            set
            {
                if (_isOnlyFetchVertical != value)
                {
                    _isOnlyFetchVertical = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsFetchBoth
        {
            get => _isFetchBoth;
            set
            {
                if (_isFetchBoth != value)
                {
                    _isFetchBoth = value;
                    OnPropertyChanged();
                }
            }
        }

        public int MinWidth
        {
            get => _minWidth;
            set
            {
                if (_minWidth != value)
                {
                    _minWidth = value;
                    OnPropertyChanged();
                }
            }
        }

        public int MinHeight
        {
            get => _minHeight;
            set
            {
                if (_minHeight != value)
                {
                    _minHeight = value;
                    OnPropertyChanged();
                }
            }
        }

        public string SaveFilePath
        {
            get => _saveFilePath;
            set
            {
                if (_saveFilePath != value)
                {
                    _saveFilePath = value;
                    OnPropertyChanged();
                }
            }
        }

        private void Fetch()
        {
            string filePath = Path.Combine(SaveFilePath, "Win10Wallpaper_Fetcher");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            FetchParam param = new FetchParam()
            {
                IsFetchBoth = IsFetchBoth,
                IsOnlyFetchHorizontal = IsOnlyFetchHorizontal,
                IsOnlyFetchVertical = IsOnlyFetchVertical,
                MinHeight = MinHeight,
                MinWidth = MinWidth
            };

            Fetcher fetcher = new Fetcher(param);
            string wallpapeDir = Fetcher.GetWallpaperDirectory();
            if (!Directory.Exists(wallpapeDir))
            {
                MessageBox.Show($"文件夹 {wallpapeDir} 不存在");
                return;
            }
            List<FileInfo> selectFiles = fetcher.GetWallpaperFiles(wallpapeDir);
            Fetcher.CopyFiles(selectFiles, filePath);
            MessageBox.Show($"成功获取 {selectFiles.Count} 张壁纸");
        }

        private void OpenFilePath()
        {
            Process.Start(Path.Combine(SaveFilePath, "Win10Wallpaper_Fetcher"));
        }

        private bool CanFetch()
        {
            return Directory.Exists(SaveFilePath);
        }

        private void SetDefaultParam()
        {
            IsOnlyFetchHorizontal = true;
            MinWidth = 1000;
            MinHeight = 500;
            SaveFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
        }

        public RelayCommand FetchWallpaperCommand => new RelayCommand(Fetch,CanFetch);

        public RelayCommand FetchAndOpenCommand=>new RelayCommand(() =>
        {
            Fetch();
            OpenFilePath();
        },CanFetch);

        public RelayCommand SetDefaultParamCommand => new RelayCommand(SetDefaultParam);
    }
}
